﻿using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace IOA.MES.Client.Common
{
    /// <summary>
    /// WinAlert.xaml 的交互逻辑
    /// </summary>
    public partial class WinAlert : Window
    {
        private int? timeout = null;
        public WinAlert(string message, int timeout = 5)
        {
            InitializeComponent();
            this.timeout = timeout;
            txtMessage.Text = message;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.Height = txtMessage.ActualHeight + 190;
            btnEmpty.Focus();
            this.Topmost = true;

            new TaskFactory().StartNew(() =>
            {
                Thread.Sleep(500);
                Dispatcher.Invoke(new Action(() =>
                {
                    if (PresentationSource.FromVisual(this) != null)
                    {
                        btnOK.Focus();
                    }
                }));
            });

            if (timeout.HasValue && timeout.Value > 0)
            {
                new TaskFactory().StartNew(() =>
                {
                    Thread.Sleep(timeout.Value * 1000);
                    Dispatcher.Invoke(new Action(() =>
                    {
                        if (PresentationSource.FromVisual(this) != null)
                        {
                            this.Close();
                        }
                    }));
                });
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
